﻿using System.Linq;

namespace Tambourine
{
    public class CliParams
    {
        public bool IsValid { get; private set; }
        public string ErrorMessage { get; private set; }

        public string InputFileName { get; private set; }
        public string OutputFileName { get; private set; }
        public string OutputTestName { get; private set; }

        public CliParams(string[] args)
        {
            IsValid = true;
            parseArguments(args);

            checkValidance();
        }

        private void parseArguments(string[] args)
        {
            string arg0 = getArgument(args, 0);

            if (arg0 == "/h" || arg0 == "/?")
                SetError();

            else
            {
                InputFileName = arg0;
                OutputFileName = getArgument(args, 1);
                OutputTestName = getArgument(args, 2);
            }
        }

        private string getArgument(string[] args, int argNum)
        {
            return (args.Count() > argNum) ? args[argNum] : null;
        }

        protected virtual void checkValidance()
        {
            if (!IsValid)
                return;

            if (InputFileName == null)
                SetError("input file path is required\n");
        }

        protected void SetError(string message = "")
        {
            IsValid = false;
            ErrorMessage = message + helpMessage;
        }

        protected string helpMessage
        {
            get
            {
                string message = "Usage: \n";
                message += System.AppDomain.CurrentDomain.FriendlyName;
                message += " [InputFileName] [OutputFileName] [OutputTestName]\n";
                message += " [InputFileName]  - NUnit result xml file path\n";
                message += " [OutputFileName] - Output xlsx file path\n";
                message += " [OutputTestName] - Tambourine test for export";

                return message;
            }
        }
    }
}
